package com.deepinblog.oosdemo.controller;

import com.deepinblog.oosdemo.ImageSize;
import com.deepinblog.oosdemo.bean.UpLoadResult;
import com.deepinblog.oosdemo.service.FileUpLoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Controller
@RequestMapping("/pic")
public class UploadController {
    @Autowired
    private FileUpLoadService fileUpLoadService;

    @PostMapping("/upload")
    @ResponseBody
    public UpLoadResult upload(@Valid  @ImageSize @RequestParam("file") MultipartFile multipartFile) {
        return fileUpLoadService.upload(multipartFile);
    }


    @GetMapping("/download")
    public void download(String name, HttpServletRequest request, HttpServletResponse response) {
        fileUpLoadService.download(name, request, response);
    }


    @PostMapping("/delete")
    @ResponseBody
    public UpLoadResult delete(String name, HttpServletRequest request, HttpServletResponse response) {
        return fileUpLoadService.delete(name, request, response);
    }


}
